set more off

use dataset, clear

keep if abs(x) < 100

local Z pcteligible2013

su `Z', det
gen Z = `Z' >=r(p50)
replace Z = . if mi(`Z')

gen Zx = Z*x
gen Zz = Z*z
gen Zzx = Z*zx

foreach x in z Z Zz {
	gen `x'B = ""
	gen `x'CI = ""
}
foreach x in z Z Zz {
	gen `x'N = ""
	gen `x'Ns = ""
	gen `x'bw = ""
	gen `x'cov = ""
	gen `x'rsq = ""
}

gen index = _n

* programs to store estimates (eststo doesn't work well with cgmboot)
capture program drop myest1
program define myest1
	args i bw cov
	mat bootresults = e(bootresults)
	svmat bootresults
	local j = 4
	foreach x in z Z Zz {
		replace `x'B = string(_b[`x'], "%20.02fc") if index == `i'
		_pctile bootresults`j' if bootresults3 == 0, nq(1000)
		replace `x'CI = "[" + string(r(r25), "%20.02fc") + ", " + string(r(r975), "%20.02fc") + "]" if index == `i'
		if `j' == 4 {
			replace `x'N = string(e(N), "%20.00fc") if index == `i'
			replace `x'Ns = string(e(N_fipsstate), "%20.00fc") if index == `i'
			replace `x'bw = "`bw'" if index == `i'
			replace `x'cov = "`cov'" if index == `i' 
			replace `x'rsq = string(e(r2), "%20.02fc") if index == `i'
		}
		local j = `j' + 1
	}	
	drop bootresults*

end

*** do the regressions ***

local nreps 500

local covariates20142010 pctwhite pct65plus pcths lmedianincome logvap2014 demvoteshare2012 registration2010 swing2012 Gub2010 Sen2010 Gub2010Sen2010 Gub2014 Sen2014 Gub2014Sen2014
local covariates20162012 pctwhite pct65plus pcths lmedianincome logvap2016 demvoteshare2012 registration2012 swing2012

cgmwildboot dregistration20142010 z Z Zz x zx Zx Zzx if abs(x) < 100, cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
myest1 1  "100 miles" "No"

cgmwildboot dregistration20142010 z Z Zz x zx Zx Zzx `covariates20142010' if abs(x) < 100, cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
myest1 2 "100 miles" "Yes"

cgmwildboot dregistration20162012 z Z Zz x zx Zx Zzx if abs(x) < 100, cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
myest1 3 "100 miles" "No"

cgmwildboot dregistration20162012 z Z Zz x zx Zx Zzx `covariates20162012' if abs(x) < 100, cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
myest1 4 "100 miles" "Yes"

* output

keep if index < 5
keep *B *CI* *N *Ns *bw *cov *rsq
l

sxpose, clear force
l

gen rowname = ""
replace rowname = "Medicaid expansion" if _n == 1
replace rowname = "High eligibility" if _n == 3
replace rowname = "Expansion X eligibility" if _n == 5
replace rowname = "Number of Counties" if _n == 7
replace rowname = "Number of States" if _n == 8
replace rowname = "Window" if _n == 9
replace rowname = "Covariates" if _n == 10
replace rowname = "R-squared" if _n == 11
order rowname 
keep if _n <= 11

#delimit;

listtex 
	using "_output/table2.tex"
	,
	replace
	type rstyle(tabular)
	head(
		\begin{tabular*}{\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{5}{c}}
		\toprule &
		\multicolumn{2}{c}{\underline{Registration}} & 
		\multicolumn{2}{c}{\underline{Registration}} \\
		& \multicolumn{2}{c}{\underline{2014-2010}} &
		\multicolumn{2}{c}{\underline{2016-2012}} \\
		&(1)&(2)&(3)&(4)\\
		\midrule
	)
	foot("\bottomrule\end{tabular*}")
	;
	
#delimit cr

l
